<?php

namespace Rebecca\RebeccaBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Rebecca\RebeccaBundle\Entity\Permisos;
use Rebecca\RebeccaBundle\Form\PermisosType;

/**
 * Permisos controller.
 *
 */
class PermisosController extends Controller
{
    /**
     * Lists all Permisos entities.
     *
     */
    public function indexAction()
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PermisosComponent */
        $component = $this->get("components.Permisos");
        $entities = $component->getAll();

        return $this->render('RebeccaBundle:Permisos:index.html.twig', array(
            'entities' => $entities
        ));
    }

    /**
     * Finds and displays a Permisos entity.
     *
     */
    public function showAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PermisosComponent */
        $component = $this->get("components.Permisos");
        
        /* @var $entity Rebecca\RebeccaBundle\Entity\Permisos */
        $entity = $component->get($id);
        
        $contrato = $entity->getIdContrato();

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Permisos entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Permisos:show.html.twig', array(
            'entity'      => $entity,
            'contrato'      => $contrato,
            'delete_form' => $deleteForm->createView(),
        ));
    }
    
    /**
     * Displays a form to select a new Employee entity.
     *
     */
    public function selectAction()
    {
        $component = $this->get("components.Contratos");

        $entities = $component->getAll();

        if (!$entities) {
            throw $this->createNotFoundException('Unable to find Contratos entity.');
        }

        
        $path = $this->generateUrl("permisos_new");
        
        return $this->render('RebeccaBundle:Contratos:select.html.twig', array(
                    'entities' => $entities,  
                    'action'  => $path,
                    'title' => 'Licencias'
                ));
               
    }    

    /**
     * Displays a form to create a new Permisos entity.
     *
     */
    public function newAction()
    {
        $id = $this->getRequest()->get('idContrato');
        /* @var $component Rebecca\RebeccaBundle\Component\ContratosComponent */
        $component = $this->get("components.Contratos");
        
        /* @var $periodosComponent \Rebecca\RebeccaBundle\Component\PeriodosComponent */
        $periodosComponent = $this->get("components.PeriodosLaborales");
        
        /* @var $contrato \Rebecca\RebeccaBundle\Entity\Contratos */
        $contrato = $component->get($id);
        
        $periodos = $periodosComponent->getTipoNomina($contrato->getIdTipoNomina()->getId());
        
        $entity = new Permisos();
        $form   = $this->createForm(new PermisosType(), $entity);

        return $this->render('RebeccaBundle:Permisos:new.html.twig', array(
            'entity' => $entity,
            'contrato' => $contrato,
            'periodos' => $periodos,
            'form'   => $form->createView()
        ));
    }

    /**
     * Creates a new Permisos entity.
     *
     */
    public function createAction()
    {
        $entity  = new Permisos();
        $request = $this->getRequest();
        
        /* @var $componentContratos Rebecca\RebeccaBundle\Component\ContratosComponent */
        $componentContratos = $this->get("components.Contratos");
        $id = $request->get("idContrato");
        
        $contrato = $componentContratos->get($id);
        
        $entity->setIdContrato($contrato);
        $entity->setFechaRegistro(date_create());
        
        $form    = $this->createForm(new PermisosType(), $entity);
        $form->bindRequest($request);

        if ($form->isValid()) {
            /* @var $component Rebecca\RebeccaBundle\Component\PermisosComponent */
            $component = $this->get("components.Permisos");
            $component->save($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('permisos'));
            
        }

        return $this->render('RebeccaBundle:Permisos:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView()
        ));
    }

    /**
     * Displays a form to edit an existing Permisos entity.
     *
     */
    public function editAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PermisosComponent */
        $component = $this->get("components.Permisos");
        
        /* @var $entity Rebecca\RebeccaBundle\Entity\Permisos */
        $entity = $component->get($id);

        $contrato = $entity->getIdContrato();
        
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Permisos entity.');
        }

        $editForm = $this->createForm(new PermisosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('RebeccaBundle:Permisos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'contrato'      => $contrato,
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Edits an existing Permisos entity.
     *
     */
    public function updateAction($id)
    {
        /* @var $component Rebecca\RebeccaBundle\Component\PermisosComponent */
        $component = $this->get("components.Permisos");
        
        /* @var $entity Rebecca\RebeccaBundle\Entity\Permisos */
        $entity = $component->get($id);

        $contrato = $entity->getIdContrato();
        
        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Permisos entity.');
        }

        $editForm   = $this->createForm(new PermisosType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

        if ($editForm->isValid()) {
            $component->update($entity);
            $component->commit();

            return $this->redirect($this->generateUrl('permisos'));
        }

        return $this->render('RebeccaBundle:Permisos:edit.html.twig', array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        ));
    }

    /**
     * Deletes a Permisos entity.
     *
     */
    public function deleteAction($id)
    {
        $form = $this->createDeleteForm($id);
        $request = $this->getRequest();
        
        /* @var $component Rebecca\RebeccaBundle\Component\PermisosComponent */
        $component = $this->get("components.Permisos");
        
        $form->bindRequest($request);

        if ($form->isValid()) {
            $entity = $component->get($id);
            
            $contrato = $entity->getIdContrato();

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find Permisos entity.');
            }

            $component->delete($entity);
            $component->commit();
        }

        return $this->redirect($this->generateUrl('permisos'));
    }

    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
}
